﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.Unity;
using enqueteJwVd.DAL;
using enqueteJwVd.Services;
using enqueteJwVd.Controllers;
using enqueteJwVd.Helpers;

namespace enqueteJwVd
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {

            filters.Add(new LogonAuthorize());
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            var container = new UnityContainer();
            container.RegisterType<IUserService, UserService>();
            //string naam moet overeenkomen met prefix van de controller.
            container.RegisterType<IController, UsersController>("Users");
            container.RegisterType<IQuestionGroupService, QuestionGroupService>();
            container.RegisterType<IController, QuestionGroupsController>("QuestionGroups");
            container.RegisterType<IRoleService, RoleService>();
            container.RegisterType<IController, RolesController>("Roles");
            container.RegisterType<IQuestionTypeService, QuestionTypeService>();
            container.RegisterType<IController, QuestionTypesController>("QuestionTypes");
            container.RegisterType<ISurveyQuestionService, SurveyQuestionService>();
            container.RegisterType<IController, SurveyQuestionsController>("SurveyQuestions");
            container.RegisterType<ISurveyService, SurveyService>();
            container.RegisterType<IController, SurveysController>("Surveys");
            container.RegisterType<IUserSurveyService, UserSurveyService>();
            container.RegisterType<IController, UserSurveysController>("UserSurveys");
            container.RegisterType<ICheckBoxOptionService, CheckBoxOptionService>();
            container.RegisterType<IController, CheckBoxOptionsController>("CheckBoxOptions");
            container.RegisterType<ISurveyQuestionGroupService , SurveyQuestionGroupService>();
            container.RegisterType<IController, SurveyQuestionGroupController>("SurveyQuestionGroup");
            container.RegisterType<ISurveyQuestionQuestionGroupService, SurveyQuestionQuestionGroupService>();
            container.RegisterType<IController, SurveyQuestionQuestionGroupsController>("SurveyQuestionQuestionGroups");
            var unityControllerFactory = new UnityControllerFactory(container);
            ControllerBuilder.Current.SetControllerFactory(unityControllerFactory);
        }
    }
}